var uploadFileSize = 1024 * 1024 * 40; //上传文件限制15m
import request from '@/common/request.js';
function getSuffix(filename) {
	let pos = filename.lastIndexOf('.');
	let suffix = '';
	if (pos != -1) {
		suffix = filename.substring(pos);
	}
	return suffix;
}

function getFileName(file, filename) {
	return (
		Math.random()
		.toString(36)
		.substring(3, 20) + new Date().getTime() +
		getSuffix(filename)
	);
}
export function uploadImg(file) {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count: 1, //默认9
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], //从相册选择
			success: function(res) {
				let Imgsrc = res.tempFilePaths[0];// 第一张图片
				let fileName = getFileName(file, Imgsrc); // 自定义上传后的文件名称
				fileUpload("image", Imgsrc, fileName).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				});
			}
		})
	})
}
export function uploadVid(file,data_mge) {
	return new Promise((resolve, reject) => {
		uni.chooseMedia({
			count: 1,
			mediaType: ['video'],
			sourceType: ['camera', 'album'],
			success: function(res) {
				let videoSrc = res.tempFiles[0];
				if (res.size > uploadFileSize) {
					uni.showToast({
						title: '文件大小超过系统上传限制：40M',
						icon: 'none',
						duration: 1000
					});
					return;
				}
				// #ifdef MP-WEIXIN
				let fileName = getFileName(file, videoSrc.tempFilePath);
				//#endif
				// #ifdef H5
				let fileName = getFileName(file, res.name);
				//#endif
				
				fileUpload("video",videoSrc, fileName,data_mge,file).then(res => {
					resolve(res)
				}).catch(err => {
					reject(err)
				});
			},
			fail: (err) => {
				reject(err)
				uni.showToast({
					title: '取消选择视频',
					icon: 'none',
					duration: 2000
				});
			}
		})
	})
}
// 文件上传
function fileUpload(type, path, stroeAs,data_mge,file) {
	uni.showLoading({
		title: '文件上传中',
		mask:true
	});
	return new Promise((resolve, reject) => {
		uni.request({
			url: request.baseUrl+'v-osscfg', //请求后台签名地址。
			data: data_mge,
			complete: res => {
				var data = res.data.data;
				var file_url =file + stroeAs;
				const uploadTask = uni.uploadFile({
					url: data.host, //后台返回的阿里云存储的上传地址
					filePath: path.tempFilePath,
					fileType: type,
					name: 'file',
					formData: {
						key:file_url, //文件名
						policy: data.policy, //后台获取超时时间
						OSSAccessKeyId: data.accessid, //后台获取临时ID
						success_action_status: '200', //让服务端返回200,不然，默认会返回204
						signature: data.signature //后台获取签名
					},
					success: res => {
						// #ifdef MP-WEIXIN
						let ext = getSuffix(path.tempFilePath);
						let original_name=path.tempFilePath;
						original_name=original_name.slice(9);
						//#endif
						// #ifdef H5
						let ext = getSuffix(path.name);
						let original_name=path.name;
						//#endif
						let upvedio_data = {
							vid: data_mge.vid,
							original_name:original_name,
							save_name:stroeAs,
							ext:ext,
							original_size:path.size,
							vedio_url:data.host,
						}
						uni.uploadFile({
						  url : request.baseUrl+'p-upvedio',
						  filePath: path.thumbTempFilePath,
						  name: 'image',
						  formData:upvedio_data,
						  success: function (uploadFileRes) {
							resolve(uploadFileRes);
						  }
						 });
					},
					fail: err => {
						resolve(err);
						// uni.hideLoading();
						// uni.showModal({
						// 	content: err.errMsg,
						// 	showCancel: false
						// });
					}
				});
			}
		})
	});
}
export default {
	uploadVid
}